﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Data;
using ICeFramework.Common.DataLayer;
using ICeFramework.Common.DataLayer.Interfaces;
using ICeFramework.Serialization;
//using ICeFramework.DataAccess.Proxy.DALServer;

namespace ICeFramework.DataAccess.Proxy
{
    public class DALProxy
    {
        private ICeFramework.DataAccess.Proxy.DALServer.RemoteDataAccessClient _client;

        public DALProxy(string url, string userName, string password)
        {
            _client = new ICeFramework.DataAccess.Proxy.DALServer.RemoteDataAccessClient();
            _client.Endpoint.Address = new EndpointAddress(url);
            _client.ClientCredentials.UserName.UserName = userName;
            _client.ClientCredentials.UserName.Password = password;
        }

        public virtual void Fill(DataTable table, EntityFilter filter)
        {
            Fill(table, filter, null, null);
        }

        public virtual void Fill(DataTable table, EntityFilter filter, IDbTransaction transaction)
        {
            Fill(table, filter, null, transaction);
        }

        public virtual void Fill(DataTable table, EntityFilter filter, EntityOrder order)
        {
            Fill(table, filter, order, null);
        }

        public virtual void Fill(DataTable table, EntityFilter filter, EntityOrder order, IDbTransaction transaction)
        {
            DataTable tmpTable = _client.Fill(table, filter, order, transaction);
            table.Rows.Clear();
            table.BeginLoadData();
            foreach (DataRow row in tmpTable.Rows)
            {
                table.ImportRow(row);
            }
            table.EndLoadData();
        }

        public virtual void Fill(DataTable table, EntityFilter filter, int pageIndex, int pageSize, out long totalRowsCount)
        {
            Fill(table, filter, null, pageIndex, pageSize, out totalRowsCount, null);
        }

        public virtual void Fill(DataTable table, EntityFilter filter, int pageIndex, int pageSize, out long totalRowsCount, IDbTransaction transaction)
        {
            Fill(table, filter, null, pageIndex, pageSize, out totalRowsCount, transaction);
        }

        public virtual void Fill(DataTable table, EntityFilter filter, EntityOrder order, int pageIndex, int pageSize, out long totalRowsCount)
        {
            Fill(table, filter, order, pageIndex, pageSize, out totalRowsCount, null);
        }

        public virtual void Fill(DataTable table, EntityFilter filter, EntityOrder order, int pageIndex, int pageSize, out long totalRowsCount, IDbTransaction transaction)
        {
            DataTable tmpTable = _client.PagedFill(out totalRowsCount, table, filter, order, pageIndex, pageSize, transaction);
            table.Rows.Clear();
            table.BeginLoadData();
            foreach (DataRow row in tmpTable.Rows)
            {
                table.ImportRow(row);
            }
            table.EndLoadData();
        }

        public virtual void Fill(DataTable table, StoredProcedureDefinition spDefinition)
        {
            Fill(table, spDefinition, null, null);
        }

        public virtual void Fill(DataTable table, StoredProcedureDefinition spDefinition, IDbTransaction transaction)
        {
            Fill(table, spDefinition, null, transaction);
        }

        public virtual void Fill(DataTable table, StoredProcedureDefinition spDefinition, EntityOrder order)
        {
            Fill(table, spDefinition, order, null);
        }

        public virtual void Fill(DataTable table, StoredProcedureDefinition spDefinition, EntityOrder order, IDbTransaction transaction)
        {
            DataTable tmpTable = _client.FillSP(table, spDefinition, order, transaction);
            table.Rows.Clear();
            table.BeginLoadData();
            foreach (DataRow row in tmpTable.Rows)
            {
                table.ImportRow(row);
            }
            table.EndLoadData();
        }

        public virtual int Count(DataTable table)
        {
            return Count(table, null);
        }

        public virtual int Count(DataTable table, EntityFilter filter)
        {
            return _client.Count(table, filter);
        }

        public virtual void Update(DataTable table, List<EntityColumn> columns)
        {
            Update(table, null, columns, null);
        }

        public virtual void Update(DataTable table, List<EntityColumn> columns, IDbTransaction transaction)
        {
            Update(table, null, columns, transaction);
        }

        public virtual void Update(DataTable table, DataRow row, List<EntityColumn> columns)
        {
            Update(table, row, columns, null);
        }

        public virtual void Update(DataTable table, DataRow row, List<EntityColumn> columns, IDbTransaction transaction)
        {
            DataTable tmpTable = null;
            try
            {
                tmpTable = _client.Update(table, BinarySerialization.Serialize(row), BinarySerialization.Serialize(columns), transaction);
            }
            finally
            {
                if (tmpTable != null)
                {
                    table.Rows.Clear();
                    table.BeginLoadData();
                    foreach (DataRow item in tmpTable.Rows)
                    {
                        table.ImportRow(item);
                    }
                    table.EndLoadData();
                }
            }
        }
    }
}
